package com.shike.palmrejection;

import android.app.KeyguardManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.util.Log;
import android.widget.Toast;

import java.util.Calendar;

public class LSBroadcastReceiver extends BroadcastReceiver {

    private SensorManager manager;
    private Context mContext;

    @Override
    public void onReceive(Context context, Intent intent) {
        mContext = context;
        switch (intent.getAction()) {
            case Intent.ACTION_SCREEN_ON: {

                Log.d("开机亮屏延时保活A", "屏幕亮了");
                FileManager.writeFile(mContext, CalendarUtil.timesAsCustomBackStr(Calendar.getInstance().getTimeInMillis(), "yyyy-MM-dd HH:mm:ss:SSS") + "保活：");

                if (isUnlocked(context)) { // 已经解锁
                    Log.d("屏幕", "已经解锁了");
                    // 关闭保活程序
                    stopListener();
                } else {
                    startListener(context);
//                    startActivity(context);
//                    Toast.makeText(mContext, "AA", Toast.LENGTH_SHORT).show();
                    // 激活传感器
                    Intent i = new Intent(context, StartActivity.class);
                    i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

                    context.startActivity(i);
                }

            }
            break;
            case Intent.ACTION_SCREEN_OFF: {
                // 关闭屏幕的时候创建保活Activity
                stopListener();
                finishActivity();
                isSaveStart = false;

                Log.d("保活", "");
//                startActivity(context);
                FileManager.writeFile(mContext, "息屏");
            }
            break;
            case Intent.ACTION_USER_PRESENT:
                Log.d("开机亮屏延时保活A", "解锁成功");
                FileManager.writeFile(mContext, "解锁成功");
                stopListener();
                finishActivity();

                break;
        }
    }

    private void startActivity(Context context) {

        meiGuang = true;
        xiangXia = true;

        synchronized (this) {
            if (!LockScreenActivity.isTop) {
                Log.d("保活", "打开保活");
                Intent intent = new Intent(context, LockScreenActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

                context.startActivity(intent);
            }
        }
    }

    private void finishActivity() {
        if (LockScreenActivity.isTop)
            mContext.sendBroadcast(new Intent("closeLSB"));
    }

    private void startListener(Context context) {
        if (manager == null)
            manager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);

        // 亮度传感器
        manager.registerListener(listener, manager.getDefaultSensor(Sensor.TYPE_LIGHT), SensorManager.SENSOR_DELAY_NORMAL);
        // 加速度传感器
        manager.registerListener(listener, manager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_NORMAL);
        // 距离传感器
//        manager.registerListener(listener, manager.getDefaultSensor(Sensor.TYPE_PROXIMITY), SensorManager.SENSOR_DELAY_NORMAL);
    }


    private void stopListener() {
        if (manager != null)
            manager.unregisterListener(listener);
    }

    private boolean isUnlocked(Context context) {
        KeyguardManager mKeyguardManager = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
        return !mKeyguardManager.isKeyguardLocked();
    }

    private boolean meiGuang = false;
    private boolean xiangXia = false;
    public boolean isSaveStart = false;

    SensorEventListener listener = new SensorEventListener() {
        @Override
        public void onSensorChanged(SensorEvent event) {

            if (isUnlocked(mContext)) {
                stopListener();
                return;
            }

            if (event.sensor == manager.getDefaultSensor(Sensor.TYPE_LIGHT)) {
                float value = event.values[0];
                if (value < Config.point(mContext)) {
                    meiGuang = true;
                } else {
                    meiGuang = false;
                }
                Log.d("距离", "当前光照强度：" + value + "lx");
                FileManager.writeFile(mContext, "光照强度：" + value);
            }

            if (event.sensor == manager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER)) {
//                float x = event.values[0];
                float y = event.values[1];
//                float z = event.values[2];

                if (y < -4) {
                    xiangXia = true;
                } else {
                    xiangXia = false;
                }
                Log.d("旋转向量", " Y：" + y);
                FileManager.writeFile(mContext, "旋转向量：" + y);
            }

            if (meiGuang && xiangXia) { // 打开保护程序
                if (!isSaveStart) {
                    Log.d("保活", "环境不允许：ISTOP-" + LockScreenActivity.isTop);
                    FileManager.writeFile(mContext, CalendarUtil.timesAsCustomBackStr(Calendar.getInstance().getTimeInMillis(), "yyyy-MM-dd HH:mm:ss:SSS") + "保活：");
                    startActivity(mContext);
                    isSaveStart = true;
                }
            } else {// 关闭保护程序
                finishActivity();
                isSaveStart = false;
            }

        }

        @Override
        public void onAccuracyChanged(Sensor sensor, int accuracy) {

        }
    };

}
